package com.mc.lp.service.impl;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.mc.lp.database.dao.IGradeYearMapperDAO;
import com.mc.lp.database.dao.IMicroCampusEntityDAO;
import com.mc.lp.database.entity.GradeYearMapperEntity;
import com.mc.lp.database.entity.MicroCampusEntity;
import com.mc.lp.database.entity.helper.GradeYearMapperHelper;
import com.mc.lp.database.exception.ConcurrentAccessException;
import com.mc.lp.database.exception.DataNotFoundException;
import com.mc.lp.service.GradeYearMapperService;
import com.mc.lp.service.MicroCampusAbstractService;
import com.mc.lp.shared.vo.GradeMappingVo;
/*
 * @author aphanda
 * @date Mar 23, 2015
 * @time 8:56:05 PM
 */
@Service
public class GradeYearMapperServiceImpl extends
MicroCampusAbstractService<GradeYearMapperServiceImpl> implements GradeYearMapperService{

	@Resource
	IGradeYearMapperDAO gradeYearMapperDAO;

	@Resource
	IMicroCampusEntityDAO microCampusEntityDAO;

		
	
	@Transactional
	public Long saveGradeMapper(GradeMappingVo vo) throws DataNotFoundException {

		MicroCampusEntity entity = microCampusEntityDAO.findById(vo.getStudentId());
		
		GradeYearMapperHelper helper = new GradeYearMapperHelper();
		GradeYearMapperEntity mapperEntity = helper.getEntity(vo);
		mapperEntity.setEntity(entity);
		try {
			gradeYearMapperDAO.saveOrUpdate(mapperEntity);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new DataNotFoundException();
		} catch (ConcurrentAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new DataNotFoundException();
		}
		
		
		return null;
	}

	@Transactional
	public void deleteGradeMapper(GradeMappingVo vo) throws DataNotFoundException {
		try {
			
			GradeYearMapperEntity en = gradeYearMapperDAO.get(vo.getId());
			
			en.setEntity(null);;
			gradeYearMapperDAO.delete(en);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new DataNotFoundException();
		} catch (ConcurrentAccessException e) {
			e.printStackTrace();
			throw new DataNotFoundException();
		}
	}

}
